package hr.bean;

import hr.dao.SalaryStructureDAO;

import java.util.ArrayList;
import java.util.Date;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;

import resources.pojo.hr_list_salary_structure;
import resources.pojo.hr_salary_structure;
import resources.pojo.hr_salary_structure_detail;
import resources.util.Tool;

@ManagedBean(name="salaryStructure")
@SessionScoped
public class SalaryStructure {
	private hr_salary_structure salaryStructure;

	//salary earning & deduction
	private ArrayList<hr_salary_structure_detail> listEarning;
	private ArrayList<hr_salary_structure_detail> listDeduction;
	
	//page manager
	private hr_salary_structure selectedSalaryStructure;
	private Date dateSearch;
	private String nameSearch;
	private String departmentSearch;
	private ArrayList<hr_list_salary_structure> listSalaryStructure;	

	private int currentPage;
	private Integer[] pages;
	
	//onload
	public void onload() {
		if(!Tool.isPostBack()){
			init();
		}
	}	
	public void onloadUpdate() {
		if(!Tool.isPostBack()){
		}
	}
	public void onloadNew() {
		if(!Tool.isPostBack()){
			if(salaryStructure == null){
				salaryStructure = new hr_salary_structure();
				listEarning = new ArrayList<hr_salary_structure_detail>();
				listDeduction = new ArrayList<hr_salary_structure_detail>();
			}
		}
	}
	
	public void init(){
		listSalaryStructure = SalaryStructureDAO.getListSalaryStructurePaging(getDepartmentSearch(), getNameSearch(),1,1, Tool.max_row);
		setPages(Tool.getPages());
	}
	
	public void initNew(){
		listEarning = new ArrayList<hr_salary_structure_detail>();
		listDeduction = new ArrayList<hr_salary_structure_detail>();
		for (int i = 0; i < 5; i++) {
			listEarning.add(new hr_salary_structure_detail());
			listDeduction.add(new hr_salary_structure_detail());
		}
	}


	public void paging(){
		setPages(Tool.getPages());
	}
	
	
	//tool navigation
	public void insert(){
		salaryStructure.setId("ATT" + Tool.generateID());
		salaryStructure.setStatus(1);
		String result = SalaryStructureDAO.action("insert",salaryStructure);
		if(result.equals("completed")){
			Tool.redirect("salaryStructure.html");
		}else if(result.equals("duplicate")){
			Tool.message("Bị trùng");
		}
	}
	
	public void update(){
		String result = SalaryStructureDAO.action("update",salaryStructure);
		if(result.equals("completed")){
			Tool.redirect("salaryStructure.html");
		}else if(result.equals("duplicate")){
			Tool.message("Bị trùng");
		}
	}
	
	public void choose(ActionEvent event){
		hr_list_salary_structure temp = (hr_list_salary_structure) event.getComponent().getAttributes().get("object");
		if(temp.getSalaryStructure()!=null){
			salaryStructure = temp.getSalaryStructure();
			salaryStructure.setEmployee(temp.getEmployee());
			Tool.redirect("update-salary-structure.html");
		}else{
			salaryStructure = new hr_salary_structure();
			salaryStructure.setEmployee(temp.getEmployee());
			initNew();
			Tool.redirect("new-salary-structure.html");
		}
	}
	
	//search
	public void search(){
		listSalaryStructure = SalaryStructureDAO.getListSalaryStructurePaging(departmentSearch, nameSearch,1,1, Tool.max_row);
		setPages(Tool.getPages());
	}
	
	
	public hr_salary_structure getSalaryStructure() {
		return salaryStructure;
	}

	public void setsalaryStructure(hr_salary_structure salaryStructure) {
		this.salaryStructure = salaryStructure;
	}

	public hr_salary_structure getSelectedSalaryStructure() {
		return selectedSalaryStructure;
	}

	public void setSelectedsalaryStructure(hr_salary_structure selectedSalaryStructure) {
		this.selectedSalaryStructure = selectedSalaryStructure;
	}

	public ArrayList<hr_list_salary_structure> getListSalaryStructure() {
		return listSalaryStructure;
	}

	public void setListSalaryStructure(ArrayList<hr_list_salary_structure> listSalaryStructure) {
		this.listSalaryStructure = listSalaryStructure;
	}
	
	public void nextPage(){
		currentPage ++;
	}

	public String getNameSearch() {
		return nameSearch==null?"":nameSearch;
	}

	public void setNameSearch(String nameSearch) {
		this.nameSearch = nameSearch;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public void setPages(Integer[] pages) {
		this.pages = pages;
	}

	public Integer[] getPages() {
		return pages;
	}


	public ArrayList<hr_salary_structure_detail> getListEarning() {
		return listEarning;
	}

	public void setListEarning(ArrayList<hr_salary_structure_detail> listEarning) {
		this.listEarning = listEarning;
	}

	public ArrayList<hr_salary_structure_detail> getListDeduction() {
		return listDeduction;
	}

	public void setListDeduction(ArrayList<hr_salary_structure_detail> listDeduction) {
		this.listDeduction = listDeduction;
	}

	public String getDepartmentSearch() {
		return departmentSearch==null?"":departmentSearch;
	}

	public void setDepartmentSearch(String departmentSearch) {
		this.departmentSearch = departmentSearch;
	}

	public Date getDateSearch() {
		return dateSearch;
	}

	public void setDateSearch(Date dateSearch) {
		this.dateSearch = dateSearch;
	}
}
